/**
 * 通用uni-app网络请求
 * 基于 Promise 对象实现更简单的 request 使用方式，支持请求和响应拦截
 */
// import qs from "qs"
import Storage from '@/config/Storage.js'
export default {
	config: {
		// baseUrl: "http://doctor.edorsun.com.cn/",
		header: {
			'Content-Type':'application/json;charset=UTF-8',
		},  
		data: {},
		method: "GET",
		//dataType: "json",  /* 如设为json，会对返回的数据做一次 JSON.parse */
		//responseType: "json",
		success() {},
		fail() {},
		complete() {}
	},
	interceptor: {
		request: null,
		response: null
	},
	request(options) {
		if (!options) {
			options = {}
		}
		// options.baseUrl = options.baseUrl || this.config.baseUrl
		options.dataType = options.dataType || this.config.dataType
		options.url =  options.url
		options.data = options.data || {}
		options.method = options.method || this.config.method
		//TODO 加密数据
		let _token = {'Authorization':Storage.localGet("QZ_TOKEN")};
		if(_token=='undefined'&&options.url.indexOf("auth/serverlogin") == -1){
			uni.redirectTo({
			    url: '/'
			});
		}
		console.log(_token)
		options.header = Object.assign({}, options.header, _token); 
		
		return new Promise((resolve, reject) => {
			let _config = null
			
			options.complete = (response) => {
				let statusCode = response.statusCode
				response.config = _config
				if (process.env.NODE_ENV === 'development') {
					if (statusCode === 200) {
						//console.log("【" + _config.requestId + "】")
					}
				}
				if (this.interceptor.response) {
					let newResponse = this.interceptor.response(response)
					if (newResponse) {
						response = newResponse
					}
				}
				// 统一的响应日志记录
				_reslog(response)
				if (statusCode === 200) { //成功
					if(response.data.code==401){
						Storage.localRemoveAll();
						uni.reLaunch({
							url:'/pages/login/login'
						})
					}else{
						resolve(response.data);
					}
				} else {
					// uni.showToast({
					// 	title: '网络异常...',
					// 	icon: 'none',
					// 	duration: 1000
					// });
					reject(response)
				}
			}

			_config = Object.assign({}, this.config, options)
			_config.requestId = new Date().getTime()

			if (this.interceptor.request) {
				this.interceptor.request(_config)
			}
			
			// 统一的请求日志记录
			_reqlog(_config)

			if (process.env.NODE_ENV === 'development') {
				//console.log("【" + _config.requestId + "】 地址：" + _config.url)
				if (_config.data) {
					//console.log("【" + _config.requestId + "】 参数：" + qs.stringify(_config.data))
				}
			}
			uni.request(_config);
		});
	},
	get(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'GET'  
		return this.request(options)
	},
	post(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	postBean(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'POST'
		options.header=this.config.header
		return this.request(options)
	},
	put(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'PUT'
		options.header=this.config.header
		return this.request(options)
	},
	delete(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options)
	},
	uploadFile(url, data, options) {
		
		if (!options) {
			options = {}
		}
		options.baseUrl = options.baseUrl || this.config.baseUrl
		options.url = options.baseUrl + url
		options.method = "FILE";
		options.formData=data;
		let _token = {'token': Storage.localGet("token") || 'undefined'};
		if(_token=='undefined'&&options.url.indexOf("permission/login") == -1){
			uni.redirectTo({
			    url: '/'
			});
		}
		options.header = Object.assign({}, options.header, _token); 
		console.info(options)
		return uni.uploadFile(options);
	},
}


/**
 * 请求接口日志记录
 */
function _reqlog(req) {
	if (process.env.NODE_ENV === 'development') {
		 
	}
	//TODO 调接口异步写入日志数据库
}

/**
 * 响应接口日志记录
 */
function _reslog(res) {
	let _statusCode = res.statusCode;
	if (process.env.NODE_ENV === 'development') {
		if (res.config.data) {
			//console.log("【" + res.config.requestId + "】 请求参数：" + qs.stringify(res.config.data))
		}
	}
	//TODO 除了接口服务错误外，其他日志调接口异步写入日志数据库
	switch(_statusCode){
		case 200:
			break;
		case 401:
			break;
		case 404:
			break;
		default:
			break;
	}
}

